fix: text classifier crash in Android - #112
Merged
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR aims to improve Android text selection/classification behavior in EnrichedTextInputView when the text contains object-replacement characters (e.g., embedded content blocks) by installing a custom TextClassifier wrapper.
Changes:
- Added
EnrichedTextClassifierto suppress selection/classification when the selection touches object-replacement characters. - Integrated the custom classifier into
EnrichedTextInputViewfor newer Android versions.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| android/src/main/java/com/swmansion/enriched/EnrichedTextInputView.kt | Installs a custom text classifier in the EditText initialization flow (but currently with API-level compatibility hazards). |
| android/src/main/java/com/swmansion/enriched/EnrichedTextClassifier.kt | Adds a TextClassifier wrapper to bypass selection/classification near replacement characters (but currently annotated too loosely for the APIs it references). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| private var typefaceDirty = false | ||
|
|
||
| private var inputMethodManager: InputMethodManager? = null | ||
| private var enrichedTextClassifier: EnrichedTextClassifier? = null |
Comment on lines
+214
to
220
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
| enrichedTextClassifier = | ||
| EnrichedTextClassifier(textClassifier).also { | ||
| setTextClassifier(it) | ||
| } | ||
| } | ||
| } |
| transformationMethod = LineSeparatorTransformationMethod() | ||
| addTextChangedListener(EnrichedTextWatcher(this)) | ||
| filters = arrayOf(NonEditableParagraphFilter(), ParagraphLimitFilter(this)) | ||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
Comment on lines
+10
to
+28
| @RequiresApi(Build.VERSION_CODES.O) | ||
| internal class EnrichedTextClassifier( | ||
| private val delegate: TextClassifier, | ||
| ) : TextClassifier { | ||
| @RequiresApi(Build.VERSION_CODES.P) | ||
| override fun suggestSelection(request: TextSelection.Request): TextSelection = | ||
| if (touchesReplacementBlock(request.text, request.startIndex, request.endIndex)) { | ||
| TextSelection.Builder(request.startIndex, request.endIndex).build() | ||
| } else { | ||
| delegate.suggestSelection(request) | ||
| } | ||
|
|
||
| @RequiresApi(Build.VERSION_CODES.P) | ||
| override fun classifyText(request: TextClassification.Request): TextClassification = | ||
| if (touchesReplacementBlock(request.text, request.startIndex, request.endIndex)) { | ||
| TextClassification.Builder().build() | ||
| } else { | ||
| delegate.classifyText(request) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
`
Summary
This pull request introduces a custom
EnrichedTextClassifierto enhance text classification behavior in theEnrichedTextInputViewcomponent, especially when handling special replacement characters. The changes ensure that text selection and classification are more robust in the presence of these characters, and that the custom classifier is used on supported Android versions.Custom Text Classification Integration:
EnrichedTextClassifierclass that wraps the defaultTextClassifierand overrides selection and classification logic to account for replacement blocks (special characters), preventing undesired behavior when these are present. (EnrichedTextClassifier.kt,EnrichedTextClassifierintoEnrichedTextInputViewfor devices running Android O (API 26) or higher, ensuring the view uses the custom classifier when available. (EnrichedTextInputView.kt,Text Classifier State Management:
Test Plan
Screenshots / Videos
Before
Screen.Recording.2026-06-27.at.12.25.15.mov
After
Screen.Recording.2026-06-27.at.12.22.03.mov
Include any visual proof that helps reviewers understand the change — UI updates, bug reproduction or the result of the fix.
Compatibility
Checklist